1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*!
Weak references to `isotope` terms
*/
use super::*;

/// A weak reference to a term in `isotope`'s term language
#[derive(Debug, Clone, Default)]
pub struct WeakId(Weak<Term>);

impl TermId {
    /// Downgrade this `TermId` to a `WeakId`
    #[inline]
    pub fn downgrade(&self) -> WeakId {
        WeakId(Arc::downgrade(&self.0))
    }
}

impl WeakId {
    /// Check whether two `WeakId`s point to the same data
    #[inline]
    pub fn ptr_eq(&self, other: &WeakId) -> bool {
        self.0.ptr_eq(&other.0)
    }
    /// Get a pointer to the data underlying this `WeakId`
    #[inline]
    pub fn as_ptr(&self) -> *const Term {
        self.0.as_ptr()
    }
    /// Attempt to upgrade this `WeakId` into a `TermId`
    #[inline]
    pub fn upgrade(&self) -> Option<TermId> {
        self.0.upgrade().map(TermId)
    }
}

impl PartialEq for WeakId {
    #[inline]
    fn eq(&self, other: &WeakId) -> bool {
        self.0.ptr_eq(&other.0)
    }
}

impl PartialEq<TermId> for WeakId {
    #[inline]
    fn eq(&self, other: &TermId) -> bool {
        self.0.as_ptr() == other.as_ptr()
    }
}

impl PartialEq<WeakId> for TermId {
    #[inline]
    fn eq(&self, other: &WeakId) -> bool {
        self.as_ptr() == other.0.as_ptr()
    }
}

impl Eq for WeakId {}

impl Hash for WeakId {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.as_ptr().hash(state)
    }
}